//
//  ISVRecognizerController.m
//  VoiceClass
//
//  Created by caoxiaobo on 2017/12/2.
//  Copyright © 2017年 YRX. All rights reserved.
//

#import "ISVRecognizerController.h"
#import <iflyMSC/IFlyISVRecognizer.h>
#import <iflyMSC/IFlySpeechError.h>
#import "MBProgressHUD.h"

@interface ISVRecognizerController () <IFlyISVDelegate>

@property (nonatomic, copy) NSString *auth_id;
@property (nonatomic, strong) IFlyISVRecognizer *isvRec;

@end

@implementation ISVRecognizerController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.auth_id = @"xiaoming111";
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[IFlyISVRecognizer sharedInstance] cancel];
}

- (IBAction)start:(UIButton *)sender {
    
//    NSDate *date = [NSDate date];
//    NSString *auth_id = [NSString stringWithFormat:@"yuanId%@", @((int)[date timeIntervalSince1970])];
//    if (auth_id.length > 18) {
//        NSInteger fromIndex = auth_id.length - 18;
//        auth_id = [auth_id substringFromIndex:fromIndex];
//    }
    
    [self.isvRec setParameter:@"train" forKey:@"sst"];
//    [self.isvRec stopListening];
    [self.isvRec startListening];
}
- (IBAction)recognize:(UIButton *)sender {
    [self.isvRec setParameter:@"verify" forKey:@"sst"];
//    [self.isvRec stopListening];
    [self.isvRec startListening];
}

- (IFlyISVRecognizer *)isvRec {
    if (!_isvRec) {
        IFlyISVRecognizer *isvRec = [IFlyISVRecognizer sharedInstance];
        isvRec.delegate = self;
        // 设置业务类型为训练
        // 设置密码类型
        [isvRec setParameter:@"1" forKey:@"pwdt"];
        // 对于文本密码和数字密码，必须设置密码的文本内容.
        // ptxt的取值为“我的地盘我做主”、“移动改变生活”、“芝麻开门”或者是从云端拉取的数字密码(每8位用“-”隔开)。
        [isvRec setParameter:@"芝麻开门" forKey:@"ptxt"];
        // 设置声纹对应的auth_id，它用于标识声纹对应的用户
        [isvRec setParameter:self.auth_id forKey:@"auth_id"];
        // 设置有效录音时间
        [isvRec setParameter:@"9000" forKey:@"vad_timeout"];
        // 末端静音检测时间，用于检测到静音自动停止录音
        [isvRec setParameter:@"700" forKey:@"vad_speech_tail"];
        _isvRec = isvRec;
    }
    return _isvRec;
}

#pragma mark - IFlyISVDelegate

/**
 {
 err = 0;
 ret = 0;
 rgn = 5;
 sst = train;
 storage = 1;
 sub = ivp;
 suc = 1;
 }
 */
-(void) onResult:(NSDictionary *)dic {
    
    NSString *sst = dic[@"sst"];
    NSNumber *err = dic[@"err"];
//    if (![err isEqualToNumber:@(0)]) {
//        return;
//    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"成功";
    hud.mode = MBProgressHUDModeText;
    [hud hideAnimated:YES afterDelay:1.5];
    
    if ([sst isEqualToString:@"train"]) {
        
    } else {
        
    }
    NSLog(@"resut****************************:\n %@", dic);
}

/*!
 *  错误码回调
 *
 *  @param errorCode 错误码
 */
-(void) onError:(IFlySpeechError *) errorCode {
    NSLog(@"resut: %@", errorCode);
    if (errorCode.errorCode != 0) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.label.text = errorCode.errorDesc;
        hud.mode = MBProgressHUDModeText;
        [hud hideAnimated:YES afterDelay:1.5];
    }
}

/*!
 *  等待结果
 */
-(void) onRecognition {
    // 设置一些UI动画
    NSLog(@"onRecognition");
}

/*!
 *  音量改变回调
 *
 *  @param volume 音量值
 */
-(void) onVolumeChanged: (int)volume {
    NSLog(@"onVolumeChanged");
}

@end
